<?php get_header(); ?>

<?php 

    //GET OPTIONS
    $canon_options_post = get_option('canon_options_post'); 

    //BUILD EXCLUDE ARRAY
    $result_cmb_hide_from_post_slider = get_post_meta( $post->ID, 'cmb_hide_from_post_slider', true);

    if (is_array($result_cmb_hide_from_post_slider)) {
        $exclude_array = array();   
        foreach($result_cmb_hide_from_post_slider as $key => $value) {
            $exclude_array[] = $key;
        }
    } else {
        $exclude_array = array();   
    }

    //GET POST ATTACHMENTS
    $args = array(
        'post_type'         => 'attachment',
        'numberposts'       => -1,
        'post_status'       => null,
        'orderby'           => 'title',
        'order'             => 'ASC',
        'post__not_in'      => $exclude_array,
        'post_parent'       => $post->ID
    );

    $post_attachments = get_posts( $args );
    //var_dump($post_attachments);

    $result_cmb_media_link = get_post_meta( $post->ID, 'cmb_media_link', true);
    $result_cmb_use_media_link = get_post_meta( $post->ID, 'cmb_use_media_link', true);
    $result_cmb_hide_feat_img = get_post_meta( $post->ID, 'cmb_hide_feat_img', true);

    //COUNT POSTS IN SLIDER
    $posts_in_slider = count($post_attachments);

    //FIRST REMOVE "ATTACHED" FEATURED IMAGE FROM EQUATION
    foreach($post_attachments as $key => $value) {
        if (get_post_thumbnail_id(get_the_ID()) == $value->ID) {
            $posts_in_slider--;
        }
     }

    //THEN COUNT RELEVANT FEATURED IMAGE OR MEDIA REPLACEMENT
    if ($result_cmb_hide_feat_img != "checked") {
        if (has_post_thumbnail(get_the_ID())) {
            $posts_in_slider++;        
        } elseif (!empty($result_cmb_use_media_link) && !empty($result_cmb_media_link)) {
            $posts_in_slider++;        
        }
            
    }



?>

		 <!-- Start Main Body -->
        <div class="main-container">
            <div class="main wrapper clearfix">
            	
            	            	
            	<!-- Start Main Content -->
                <div class="main-content">
                    <div id="post-<?php the_ID(); ?>" <?php post_class("clearfix"); ?>>
                		
                        <!-- BEGIN LOOP -->
                        <?php while ( have_posts() ) : the_post(); ?>

                    		<!-- TITLE AND META -->
                            <h2><?php the_title(); ?></h2>
                    		<h6 class="meta"><?php _e('Posted on','loc_canon'); ?> <?php echo mb_localize_datetime(get_the_time(get_option('date_format'))); ?></h6>
                    		
                            <!-- FEATURED IMAGE -->
                            <?php 
                                if ($posts_in_slider > 0) {
                                ?>
                                    <div class="flexslider flexslider_single">
                                        <ul class="slides">
                                            <?php
                                                if ($result_cmb_hide_feat_img != "checked") {
                                                    if (!empty($result_cmb_use_media_link) && !empty($result_cmb_media_link)) {
                                                        echo "<li>";
                                                        echo $result_cmb_media_link;
                                                        echo "</li>";
                                                    } elseif ( has_post_thumbnail(get_the_ID()) && get_post(get_post_thumbnail_id(get_the_ID())) ) { 
                                                        $post_thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()),'full');
                                                        $img_post = get_post(get_post_thumbnail_id(get_the_ID()));
                                                        $featured_img_width = 741;
                                                        printf("<li><a title='%s' href='%s'><img width='%d' src='%s'></a></li>", esc_attr($img_post->post_title), esc_url($post_thumbnail_src[0]), esc_attr($featured_img_width), esc_url($post_thumbnail_src[0]));
                                                    }
                                                }
                                            ?>

                                        <?php 
                                            for ($i = 0; $i < count($post_attachments); $i++) {
                                                if (get_post_thumbnail_id(get_the_ID()) != $post_attachments[$i]->ID) {
                                                    $attachment_src = wp_get_attachment_image_src($post_attachments[$i]->ID, 'full');
                                                    $img_post = get_post($post_attachments[$i]->ID);
                                                    ?>  
                                                        <li>
                                                          <a href='<?php echo $attachment_src[0]; ?>' title='<?php echo $img_post->post_title; ?>'><img src="<?php echo $attachment_src[0]; ?>"></a>
                                                        </li>
                                                    <?php
                                                }
                                            }

                                        ?>
                                        </ul>
                                    </div>

                                <?php    
                                }
                            ?>

                            <!-- MAIN CONTENT AND WP_LINK_PAGES -->
                            <div id="the-content"><?php the_content(); ?></div>
                            <div class="link-pages"><?php wp_link_pages(array('before' => '<p>' . __('Pages:','loc_canon'))); ?></div>
                    		
                            <!-- POST PAGINATION -->    
                            <?php if ($canon_options_post['show_post_nav'] == "checked") get_template_part('inc/templates/template_post_pagination'); ?>   
                            
                            <!-- COMMENTS -->    
                            <?php if ($canon_options_post['show_comments'] == "checked") comments_template( '', true ); ?>
                            
                        <?php endwhile; ?>
                        <!-- END LOOP -->
                			                                   
					<!-- Finish Main Content -->
                	</div>    
                </div>
				
                <!-- SIDEBAR -->
                <?php get_sidebar(); ?>
				

        	</div> <!-- #main -->
        </div> <!-- #main-container -->


		
<?php get_footer(); ?>